<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { gamesApi } from '@/api/controller';
  import { ToastSuccess, ToastWarning, Toastloading, allNumber } from '@/utils/public';
  import serachGift from '@/components/serachGift/index.vue'
  const formLabelWidth = '160px'
  const dataForm = ref(),
  serachGiftRef = ref();
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
    giftInfo:{},
    pool_id:0,
    gift_type:0,
    p_gift_id:0,
    pool_config_id:0,
    sp_id:0,
    p_pool_list:[] as Array<any>,
    pool_list:[] as Array<any>

  })
  interface formQuert {
    pool_id:number
    pool_config_id:number
    child_gift_id:number
    gift_num:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    child_gift_id: [{ required: true, message: "请选择礼物", trigger: "blur" }],
    gift_num: [{ required: true, message: "请输入礼物数量", trigger: "blur" }],
  }
  const handleSubmit = async()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const poolObj = action.pool_list.find(res=>res.child_gift_id == form.child_gift_id) || {child_gift_id:0,sp_id:0}
      if(poolObj.child_gift_id && poolObj.sp_id != action.sp_id) return ToastWarning('配置中存在当前礼物')
      const loading = Toastloading()
      let submitSet = false
      if(action.sp_id) {
        await gamesApi.wishLuckPoolConfigChildEdit({
          sp_id:action.sp_id,
          p_gift_id:action.p_gift_id,
          insertStr:JSON.stringify([{
            ...form,
            pool_id:action.pool_id,
            pool_config_id:action.pool_config_id
          }])
        }).then(()=>{
          ToastSuccess('编辑礼物配置成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }else {
        await gamesApi.wishLuckPoolConfigChildAdd({
          gift_type:action.gift_type,
          p_gift_id:action.p_gift_id,
          insertStr:JSON.stringify([{
            ...form,
            pool_id:action.pool_id,
            pool_config_id:action.pool_config_id
          }])
        }).then(()=>{
          ToastSuccess('新增礼物配置成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        $emit('updateList')
        closeDialog()
      }
    })
  }
  const openDialog = (query:any={})=> {
    const {
      sp_id=0, pool_id=0, pool_config_id=0, gift_type=0,
      p_gift_id=0, child_gift_id='', gift_num='', p_pool_list=[], pool_list=[] } = query
    action.pool_id = pool_id
    action.pool_config_id = pool_config_id
    action.gift_type = gift_type
    action.p_gift_id = p_gift_id
    action.p_pool_list = p_pool_list
    action.pool_list = pool_list
    action.sp_id = sp_id
    Object.assign(form,{
      child_gift_id,
      gift_num
    })
    action.title = `${sp_id?`编辑`:'新增'}礼物配置`
    action.dialogFormVisible = true
    nextTick(()=>{
      const list = p_pool_list.map((res:any)=>{
        const { gift_id, gift_name } = res
        return {
          gift_id,
          name:gift_name
        }
      })
      serachGiftRef.value.setList(list)
    })
  }
  const closeDialog = ()=>{
    action.pool_id = 0
    action.pool_config_id = 0
    action.gift_type = 0
    action.p_gift_id = 0
    action.sp_id = 0
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="40%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="礼物" prop="child_gift_id">
          <serachGift
            ref="serachGiftRef"
            :externalApi="true"
            v-model:gift_id="form.child_gift_id" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" prop="gift_num" label="礼物数量">
          <el-input @input="allNumber(form,'gift_num')" v-model="form.gift_num" placeholder="请输入礼物数量" />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>